package com.toprand.lockscreenvideo;

import android.annotation.SuppressLint;
import android.app.KeyguardManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.PowerManager;
import android.util.Log;

/**
 * Created by mycoder on 2017/11/17.
 * 熄屏广播
 */

public class ScreenReceive extends BroadcastReceiver {

    private SharedPreferences sp;
    private OnScreenLockedListener listener;

    public ScreenReceive(OnScreenLockedListener listener){
        this.listener = listener;
    }

    @SuppressLint("Wakelock")
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        Log.e("Wakelock", "onReceive " + action);
        sp = context.getSharedPreferences(context.getPackageName(), Context.MODE_PRIVATE);
        if (action != null && action.equals(Intent.ACTION_SCREEN_OFF)){
            Log.e("Wakelock", "unlock");
            // 获取PowerManager的实例
            PowerManager pm = (PowerManager) context
                    .getSystemService(Context.POWER_SERVICE);
            // 得到一个WakeLock唤醒锁
            PowerManager.WakeLock mWakelock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK
                    | PowerManager.ACQUIRE_CAUSES_WAKEUP
                    | PowerManager.ON_AFTER_RELEASE, "bright");
            if (mWakelock.isHeld()) {
                // 释放屏幕,是屏幕休眠
                mWakelock.release();
            }
            if (!mWakelock.isHeld()) {
                Log.e("Wakelock", "!mWakelock.isHeld()");
                // 唤醒屏幕
                mWakelock.acquire();
//                mStartActivity(context, ProductVideoActivity.class);
                if (listener != null){
                    listener.onLocked(context);
                }
                mWakelock.release();
            }
            // 获得一个KeyguardManager的实例
            KeyguardManager keyguardManager = (KeyguardManager) context
                    .getSystemService(Context.KEYGUARD_SERVICE);
            KeyguardManager.KeyguardLock keyguardLock = keyguardManager.newKeyguardLock("unLock");
            if (keyguardManager.inKeyguardRestrictedInputMode()) {
                // 解锁键盘
                keyguardLock.disableKeyguard();
            }
        }
    }
}